import { Maths, Move, Sets, Vector2, _logic, ccclass } from "../../../main/script/Main";
import { EEntityType } from "../../../main/script/module/define/LogicDefine";
import { Entity } from "./Entity";

const v2T = new Vector2()

@ccclass("MoveAnimEntity")
export class MoveAnimEntity extends Entity {

    public entityType = EEntityType.Effect
    public isPut = true
    public isUpdateStateMackine = true
    public isDestroy = false
    public isMoving() { return false }


    public posY: number[] = null!
    public endIndex: number = -1
    public width: number = -1
    public animComplete: () => void = null!

    private _move = new Move()

    protected onStateEnterLoad(): void {
        super.onStateEnterLoad()

        this._move.init(this.node, false, 100)

        this.setSize(this.width, _logic.cubeSize.height)

        v2T.x = 0
        v2T.y = Sets.random(this.posY)
        this.setPositionXY(v2T, false)
    }


    protected onStateEnterRun(): void {
        super.onStateEnterRun()

        let _indexs: number[] = []
        let _len = Maths.minToMax(2, 5)

        for (let i = 0; i < _len; i++) {
            let index = Maths.zeroToMax(this.posY.length)
            if (Sets.pop(_indexs, false) != index)
                _indexs.push(index)
        }
        if (Sets.pop(_indexs, false) != this.endIndex) _indexs.push(this.endIndex)

        this._move.setRunData(_indexs.length, (runData, index) => {
            runData.target.x = 0
            runData.target.y = this.posY[_indexs[index]]

            runData.speed = Maths.minToMax(500, 1500)
        })

        this._move.run(() => {
            this.scheduleOnce(() => {
                this.scene.entityMgr.remove(this)
            }, .2)
            this.animComplete()
        })

    }


    protected onStateUpdateRun(): void {
        super.onStateUpdateRun()
        this._move.onUpdate()
    }


}